﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
public class Lifebar_UGUI : MonoBehaviour {
    public Slider healthSlider;
    public float currentHp = 10f;
    private float changedHp = 10f;
    private Rect hpChangeRect;
    private Rect hpChangeLabelRect;
    private Rect addBtnRect;
    private Rect decBtnRect;
    private string hpChangeStr = "1";
    private int hpChange;
 
    void Start () {
        addBtnRect = new Rect(10, 50, 40, 20);
        decBtnRect = new Rect(60, 50, 40, 20);
        hpChangeRect = new Rect(10, 80, 40, 20);
        hpChangeLabelRect = new Rect(55, 80, 40, 20);
	}

    void Update()
    {
        this.transform.LookAt(Camera.main.transform.position);
    }
 
    private void OnGUI() {
        hpChangeStr = GUI.TextArea(hpChangeRect, hpChangeStr);
        hpChange = 1;
        int.TryParse(hpChangeStr, out hpChange);
        GUI.Label (hpChangeLabelRect, "HP");
        if(GUI.Button(addBtnRect, " + ")) 
            addBlood(hpChange);
        else if(GUI.Button(decBtnRect, " - ")) 
            DecBlood(hpChange);

        currentHp = Mathf.Lerp(currentHp, changedHp, 0.1f);
        healthSlider.value = currentHp;
    }

    public void addBlood(float num) {
        changedHp = changedHp + num > 10f? 10f : changedHp + num;
    }
 
    public void DecBlood(float num) {
        changedHp = changedHp - num < 0f? 0f : changedHp - num;
    }
}

